﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SecureCookie.Implementation.Helper;

namespace SecureCookie.Implementation.Hook.Impl
{
    class WebContextTraceLogger : ILogger
    {
        public void LogVerbose(Func<string> message, Func<object[]> args)
        {
            if (HttpContext.Current != null)
            {
                HigherOrderFunctions.TryAndSwallow(
                    () => HttpContext.Current.Trace.WriteIfEnabled(message, args),
                    (ex) => LogException(ex));
            }
        }

        public void LogException(Exception exception)
        {
            if (HttpContext.Current != null)
            {
                HigherOrderFunctions.TryAndSwallow(
                    () => HttpContext.Current.Trace.WriteIfEnabled(() => "An exception was thrown: {0}", () => new object[] { exception.RepresentAsStringRecursively() }),
                    (ex) => { });
            }
        }
    }
}
